﻿using DataModels.Dto;
using DataModels.Dto.BIll;
using DataModels.Entities.Bill;
using DataModels.Enums.Bill;
using SqlSugar;
using Utils;

namespace Services.Bill
{
    public class BillService(SqlSugarClient db) : BaseService(db)
    {
        public ResponseModel GetBillInfo(GetBillInfo info)
        {
            var bill = db.Queryable<BillMain>()
                .Where(b => b.UserUUID == UserUUID)
                //类型为付款
                .Where(b => b.PayType == PaymentType.Payment)
                //状态为支付成功
                .Where(b => b.PayStatus == PaymentStatus.Success)
                .WhereIF(info.BusinessType.HasValue, b => b.BusinessType == info.BusinessType)
                .WhereIF(info.StartTime.HasValue, b => b.CreateTime >= info.StartTime)
                .WhereIF(info.EndTime.HasValue, b => b.CreateTime <= info.EndTime)
                .OrderBy(b => b.CreateTime,OrderByType.Desc)
                .ToList()
                ;
            var response = bill.Select(dto => new GetBillInfoResponse
            {
                BillId = dto.UUID,
                UserId = dto.UserUUID,
                UserName = dto.UserName,
                BusinessType = dto.BusinessType.GetDescription(),
                PayWay = dto.PayWay.GetDescription(),
                PayNo = dto.PayNo,
                PayType = dto.PayType.GetDescription(),
                PayAmount = dto.PayAmount,
                PayRemark = dto.PayRemark,
                PayStatus = dto.PayStatus.GetDescription(),
                ExtraData = dto.ExtraData,
                PayTime = dto.PayTime,
                Remark = dto.Remark
            });
            return Success(response);
        }

        public ResponseModel AddBill(BillMain billMain)
        {
            billMain.CreateTime = DateTime.Now;
            db.Insertable(billMain).ExecuteCommand();
            return Success("账单添加成功");
        }
    }
}
